package com.ht.controller;

import com.ht.entity.User;
import com.ht.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Htao
 * 邮箱:hutao1993310@163.com
 * 2018/5/11
 */

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService iUserService;

    /**
     * 查询所有的用户*/
    /**
     * 查询所有用户
     * @param name
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String listUser(String name, Model model){
      List<User> userList =  iUserService.listUserUser();
        model.addAttribute("userList",userList);
      return "userList";
    }
    /*根据id删除用户*/

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @RequestMapping("/deleteUserById")
    public String deleteUserById(Integer id){
        iUserService.deleteUserById(id);
        return "redirect:list";
    }
    /*注册用户，添加用户*/
    @RequestMapping("/addUser")
    @ResponseBody
    public String addUser(User user ){

        iUserService.addUser(user);
        return "redirect:list";
    }
    /*获取注册页面*/
    @RequestMapping("/gitRegisterUI")
    public String register(String a) {
        return "register";

    }

    /**
     * 获取修改页面并传值
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/gitUserEditUI")
    public String gitUserEditUI(Integer id ,Model model){
       User user = iUserService.gitUserEditUI(id);
        model.addAttribute("user",user);
        return "userEdit";
    }


    @RequestMapping("/updateUser")
    public String updateUser(User user){

        iUserService.updateUser(user);
        return "redirect:list";
   }

   @RequestMapping("/deleteUserAll")
    public String deleteUserAll(Integer[] ids){
       System.out.println(ids.length);
       for (Integer id : ids) {

       iUserService.deleteUserById(id);
       }
        return "redirect:list";
   }
   @RequestMapping("/login")
   @ResponseBody
    public String login(String username,String password){
       iUserService.login(username,password);
       Map<String,Object> resMap = new HashMap<String, Object>();
       //校验用户名
//       if( == null){
//           resMap.put("msg","error");
//       } else{
//           resMap.put("msg","success");
//       }


       return null;
   }


}
